<?php
/**
 * Created by PhpStorm.
 * User: suruixiang
 * Date: 2017/10/11
 * Time: 下午5:11
 */
namespace app\admin\controller;

use think\Request;

class Channel extends Common
{
    //渠道列表
    public function chLists(Request $request)
    {
        $where = [];
        //状态
        if ($request->param('status')) {
            $where['status'] = $request->param('status', 1, 'intval');
            $this->assign('status', $where['status']);
        }
        //生成时间
        if ($request->param('starttime') && $request->param('endtime')) {
            $starttime = $request->param('starttime', '', 'trim');
            $endtime = $request->param('endtime', '', 'trim');
            $where['create_time'] = ['between', [
                strtotime($starttime),
                strtotime($endtime) + 86400,
            ]];
            $this->assign('starttime', $starttime);
            $this->assign('endtime', $endtime);
        } elseif ($request->param('starttime')) {
            $starttime = $request->param('starttime', '', 'trim');
            $where['create_time'] = [
                'egt',
                strtotime($starttime),
            ];
            $this->assign('starttime', $starttime);
        } elseif ($request->param('endtime')) {
            $endtime = $request->param('endtime', '', 'trim');
            $where['create_time'] = [
                'elt',
                strtotime($endtime) + 86400,
            ];
            $this->assign('endtime', $endtime);
        }
        //课程名
        if ($request->param('courses_title')) {
            $courses_title = $request->param('courses_title', '', 'trim');
            $where['courses_title'] = $courses_title;
            $this->assign('courses_title', $courses_title);
        }
        $channelModel = new \app\admin\model\Channel();
        $data = $channelModel->getAllLists($where);
        $this->assign('lists', $data['lists']);
        $this->assign('page', $data['page']);
        return $this->fetch();
    }
    //变更渠道状态
    public function chUpstatus(Request $request)
    {
        $channelModel = new \app\admin\model\Channel();
        if ($request->isPost()) {
            $id = $request->param('id', 0, 'intval');
            if (!$id) {
                $this->error('参数错误');
            }
            $data['status'] = $request->post('status', 0, 'intval');
            if ($channelModel->updateData(['id'=>$id], $data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
}